#!/bin/bash

message=''
tag=''
specFilePath=''

printHelp(){
	echo 'Usage: hotfix [-e {public-key-path}] -f [{folder-path}|{zip-file-path}] [-i|-a]'
	echo ' -V {app-version} -v {zip-version} [-p {page-name}] [-s {source-type}] [-d app-identifier]'
	echo 'eg: '
	echo '    hotfix -f /Users/WuShiHai/Desktop/HotFix/patch -V 4.5.2 -e /Users/WuShiHai/Desktop/HotFix/rsa_public_key.pem -v 0.0.3 -c dev -s hotfix -d com.hs.yjseller'
	echo '    hotfix -f /Users/WuShiHai/Desktop/HotFix/patch -V 4.5.2 -e /Users/WuShiHai/Desktop/HotFix/rsa_public_key.pem -v 0.0.3'
	echo '    hotfix -f /Users/WuShiHai/Desktop/HotFix/patch -V 4.5.2 -v 0.0.3 -a'
	echo '    hotfix -f /Users/WuShiHai/Desktop/HotFix/patch.zip -V 4.5.2 -v 0.0.3 -a'
	echo ''
	printf "%4s %-8s %s\n" ' ' '-h' 'optional, help'
	printf "%4s %-8s %s\n" ' ' '-e' 'optional, RSA privateKey.pem path,when use -e, -f should {folder-path}'
	printf "%4s %-8s %s\n" ' ' '-k' 'optional, AES password , -k pair with -e, default ${passwordOfAES}'
	printf "%4s %-8s %s\n" ' ' '-f' 'file path or folder path,if folder path,then zip folder to upload'
	printf "%4s %-8s %s\n" ' ' '-i' "optional, platform is iOS ，\"-i\" and \"-a\" mutex, default ${platform}"
	printf "%4s %-8s %s\n" ' ' '-a' "optional, platform is Android ，\"-i\" and \"-a\" mutex, default ${platform}"
	printf "%4s %-8s %s\n" ' ' '-V' 'app version, eg 4.5.2'
	printf "%4s %-8s %s\n" ' ' '-v' 'zip version, eg 0.0.2'
	printf "%4s %-8s %s\n" ' ' '-s' "optional, sourceType, eg.\"hotfix\",\"hybrid\",etc, default \"${sourceType}\""
	printf "%4s %-8s %s\n" ' ' '-d' "optional, appIdentifier, eg.\"com.hs.yjseller\",\"com.weimob.mdstore\",etc, default \"${appIdentifier}\""
	printf "%4s %-8s %s\n" ' ' '-c' "optional, server, eg.\"dev\", \"pl\", \"release\", default \"${config}\""
	echo Created by 萌店 on 2/14/17.
	echo Copyright © 2017 WeiMob. All rights reserved.
}

stringLastCharacter(){
	string=$1
	if [[ ${#string} > 0 ]]; then
		echo ${string:${#string} - 1:${#string}}
	else
		echo ''
	fi
}
stringFirstCharacter(){
	string=$1
	if [[ ${#string} > 0 ]]; then
		echo ${string:0:1}
	else
		echo ''
	fi
}
stringSubStr(){

    string_a=$1
    string_b=$2

    if [[ ${string_a/${string_b}//} == $string_a ]]
    then
        return 0
    else
        return 1
    fi
}

fileFullPath(){
	folderPath=$1
	fileName=$2
	if [[ `stringLastCharacter ${folderPath}` == '/' ]]; then
		echo ${folderPath}${fileName}
	else
		echo ${folderPath}'/'${fileName}
	fi
}
fileIsExist(){
	if [[  -f $1 ]]; then
		return 1
	else
		return 0
	fi
}

handleParameterError(){
	if [[ x${message} == x ]]; then
		echo "less paramerter -m"
		exit
	fi
	if [[ x${tag} == x ]]; then
		echo "less paramerter -t"
		exit
	fi
	if [[ x${specFilePath} == x ]]; then
		echo "less paramerter -f"
		exit
	fi
}

isContainsHelpCommand=false
isParameterError=false

while getopts "m:t:f:" arg #选项后面的冒号表示该选项需要参数
do
        case $arg in
        	m)
            	message=${OPTARG}
                ;;
            t)
            	tag=${OPTARG}
                ;;
            f)
                specFilePath=${OPTARG}
                ;;
            ?)  #当有不认识的选项的时候arg为?
        # exit 1

        esac
done

if [ ${isContainsHelpCommand} = true ] ; then
	printHelp
	exit
fi

#检查参数
handleParameterError

`echo git add .`
`echo git commit -m ${message}`
`echo git pull origin master`
`echo git push origin master`
`echo git tag "${tag}"`
`echo git push --tag`
`echo pod repo --verbose --allow-warnings push  weimob-scm-loan-wfspecs ${specFilePath}`

